
class Node(object):
    def __init__(self, data = None):
        self.data = data
        self.next = None


class List(object):
    def __init__(self):
        self.hader = Node()
        self.leng = 0

    def append(self, data):
        self.insert = (self.leng, data)

    def insert(self, pos, data):
        assert pos >= 0 and pos <= self.leng

        tmpPtr = self.hader
        while pos > 0:
            tmpPtr = tmpPtr.next
            pos -= 1

            newNode = Node()

            newNode.data = data
            newNode.next = tmpPtr.next
            tmpPtr.next = newNode
            self.leng += 1

    def remove(self, pos):
        assert pos < self.leng

        tmpPtr = self.hader
        while pos > 0:
            tmpPtr = tmpPtr.next
            pos -= 1

        if tmpPtr.next is not None:
            delNode = tmpPtr.next
            tmpPtr.next = delNode.next
            self.leng -= 1

    def foreach(self):
        tmpPtr = self.hader.next
        while tmpPtr is not None:
            yield tmpPtr.data
            tmpPtr = tmpPtr.next

    def find(self):
        pass

if __name__ == "__main__":
    l = List()
    l.append(1)
    l.append(2)
    l.append(3)

    for x in l.foreach():
        print(x)

    print("Insert new data 4")
    for x in l.foreach():
        print(x)

    print("Remove the 2th element")
    l.remove(1)
    for x in l.foreach():
        print(x)
